package com.zbest.zpay.acctrans.component;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.zbest.common.lang.base.Money;
import com.zbest.zpay.acctrans.common.exception.AccTransRuntimeException;
import com.zbest.zpay.acctrans.common.util.BalanceTagUtils;
import com.zbest.zpay.acctrans.dal.mapper.InnerBalanceMapper;
import com.zbest.zpay.acctrans.dal.model.InnerBalance;
import com.zbest.zpay.acctrans.enums.biz.AcctransBizCode;


public class CreateInnerAccountBalanceComponent {
    private static final Logger logger = LoggerFactory
            .getLogger(CreateInnerAccountBalanceComponent.class);

    /**
     * 内部账户余额仓储
     */
    @Autowired
    private InnerBalanceMapper innerBalanceMapper;

    public void execute(String inActTyp, String inActNo, String iacBalDrt, String actDat, String jrnNo) {
        logger.info("[create innner account!]");
        if (StringUtils.isBlank(inActTyp)) {
            logger.info(jrnNo + "[inActTyp不能为空]");
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "[inActTyp不能为空]");
        }
        if (StringUtils.isBlank(inActNo)) {
            logger.info(jrnNo + "[inActNo不能为空]");
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "[inActNo不能为空]");
        }
        if (StringUtils.isBlank(iacBalDrt)) {
            logger.info(jrnNo + "[IAC_BAL_DRT不能为空]");
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "[IAC_BAL_DRT不能为空]");
        }

        logger.info("[检查该账户余额是否存在]");
        InnerBalance qryInnerBalance = innerBalanceMapper.qryByInActNo(inActNo);
        if (qryInnerBalance != null) {
            logger.info("[该账户余额信息已存在]");
            return;
        }

        Money zero = new Money(0, Money.DEFAULT_CURRENCY);
        String tagCreatRet = BalanceTagUtils.createBalanceTag(inActNo, zero);
        InnerBalance innerAccountBalance = new InnerBalance();
        innerAccountBalance.setInActNo(inActNo);
        innerAccountBalance.setInActTyp(inActTyp);
        innerAccountBalance.setBalTag(tagCreatRet);
        innerAccountBalance.setUpdDat(actDat);
        innerAccountBalance.setUpdJrn(jrnNo);
        innerAccountBalance.setIacBalDrt(iacBalDrt);
        innerBalanceMapper.insert(innerAccountBalance);
        logger.info("[创建内部账户" + inActTyp + "余额信息成功]");
    }
}
